package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.ProductBean;
import com.project.mapper.IProductMapper;
import com.project.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductServiceImpl implements IProductService {

    @Autowired
    private IProductMapper productMapper;
    @Override
    public IPage<ProductBean> findByItem(Integer pageNO, String name, Integer startNum, Integer endNum, String salePrice, String type) {
        QueryWrapper qw = new QueryWrapper();
        if (name!=null&&name.length()!=0){
            qw.like("p_name",name);
        }
        if (startNum!=null){
            qw.ge("p_num",startNum);
        }
        if (endNum!=null){
            qw.le("p_num",endNum);
        }
        if (salePrice!=null&&salePrice.length()!=0){
            if (salePrice.equals("未定义销售价")){
                qw.isNull("p_sellPrice");
            }
            if (salePrice.equals("定义销售价")){
                qw.isNotNull("p_sellPrice");
            }
        }
        if (type!=null&&type.length()!=0){
            qw.eq("p_type",type);
        }
        return productMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public ProductBean findById(String id) {
        return productMapper.selectById(id);
    }

    @Override
    public void updateSalePrice(String id, Integer sellMoney) {
        ProductBean productBean = productMapper.selectById(id);
        productBean.setSellPrice(sellMoney);
        productMapper.updateById(productBean);
    }

    @Override
    public IPage<ProductBean> findBySaleCar(Integer pageNO, String name, String type) {
        QueryWrapper<ProductBean> qw = new QueryWrapper();
        if (name!=null&&name.length()!=0){
            qw.like("p_name",name);
        }
        if (type!=null&&type.length()!=0){
            qw.eq("p_type",type);
        }

        return productMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
        public List<String> findAllType() {
            return productMapper.findAllType();
    }



}
